package com.yang.leather.common.hibernate;

import java.io.Serializable;

import org.hibernate.EmptyInterceptor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.type.Type;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yang.leather.core.BaseServletContext;

public class TreeInterceptor extends EmptyInterceptor {
	private static final long serialVersionUID = 1L;
	
	protected SessionFactory sessionFactory;

	protected SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			WebApplicationContext wac = WebApplicationContextUtils
					.getRequiredWebApplicationContext(BaseServletContext.getServletContxt());
			sessionFactory = (SessionFactory) wac.getBean("sessionFactory",
					SessionFactory.class);
		}
		return sessionFactory;
	}

	protected Session getSession() {
		return getSessionFactory().getCurrentSession();
	}
	
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		return true;
	}
}
